package cn.com.forever.string;

public class 替换空格 {
    public static void main(String[] args) {
        String s = "We are happy.";
        replaceSpace(s);
        //ss.toString();
    }

    public static void replaceSpace (String s) {
        char[] ch  = s.toCharArray();
        int firstlen = ch.length -1;
        //System.out.println(firstlen);  12
        int count = 0;
        for (int i = 0; i < ch.length; i++) {
            if(ch[i] == ' '){
                count++;
            }
        }
        int secondlen = firstlen+2*count;
        char[] str = new char[ch.length+2*count];
        while(firstlen >=0 && secondlen >= firstlen){
            if(ch[firstlen] == ' '){
                str[secondlen--] = '0';
                str[secondlen--] = '2';
                str[secondlen--] = '%';
                //firstlen--;
            }
            else {
                str[secondlen--] = ch[firstlen];
                //firstlen--;
            }
            firstlen--;
        }
//        for (int i = 0; i < str.length; i++) {
//            System.out.println(str[i]);
//        }
//        StringBuffer res = new StringBuffer();
//        for (int i = 0; i < str.length; i++) {
//            res.append(str[i]);
//        }
//        res.toString();
    }
}
